package com.mnnu.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;

import java.io.IOException;

//@WebFilter(urlPatterns = "/*") //配置Filter   //拦截所有资源
public class DemoFilter implements Filter{ //Filter是javaweb的三大组件 使用需在springboot启动类上加上@ServletComponentScan组件
    //初始化方法，只调用一次
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init");
        Filter.super.init(filterConfig);
    }

    //拦截到请求后调用，调用多次
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //放行前逻辑
        System.out.println("Demo放行前逻辑");

        //放行
        filterChain.doFilter(servletRequest, servletResponse);

        //放行后逻辑
        System.out.println("Demo放行后逻辑");
    }

    //销毁方法，只调用一次
    @Override
    public void destroy() {
        System.out.println("destroy");
        Filter.super.destroy();
    }
}
